package o;

import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean.a;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.Executor;

import AsyLoader.e;

public class cwf implements e {
    private Map<String, LinkedList<AsyLoader>> ˎ = new HashMap();
    private WeakReference<AsyLoader> ॱ;

    public static String ˏ() {
        File file = new File(ContentManager.getInstant().getContext().getCacheDir().getPath() + File.separator + "httpCache" + File.separator);
        if (!(file.exists() || file.mkdir())) {
            cvp.ˎ.ˊ("ServerAgentImpl", "cacheRoot mkdir failed!");
        }
        return file.getAbsolutePath() + File.separator;
    }

    public static void ˋ() {
        dnw.ˊ(new File(ˏ()));
        cvp.ˎ.ˋ("ServerAgentImpl", "clear all http cache completed");
    }

    protected void ˏ(AsyLoader o_cwa) {
        if (o_cwa.ॱˊ() != null) {
            LinkedList linkedList = (LinkedList) this.ˎ.get(o_cwa.ॱˊ());
            if (linkedList == null) {
                linkedList = new LinkedList();
                this.ˎ.put(o_cwa.ॱˊ(), linkedList);
            }
            if (o_cwa.ˎ().getRequestType() == o.cvu.RequestStrategy.REQUEST_CACHE && linkedList.size() > 0) {
                ˋ(linkedList, o_cwa.ॱˊ(), o_cwa.ˎ().getMethod_());
            }
            linkedList.addLast(o_cwa);
            cvp.ˎ.ˋ("ServerAgentImpl", "cacheSessionTask, sessionCacheSize:" + this.ˎ.size() + ", method:" + o_cwa.ˎ().getMethod_() + ", requestType:" + o_cwa.ˎ().getRequestType());
        }
    }

    private void ˋ(LinkedList<AsyLoader> linkedList, String str, String str2) {
        cvp.ˎ.ˋ("ServerAgentImpl", "clearTimeoutTask, method:" + str2);
        Iterator it = linkedList.iterator();
        while (it.hasNext()) {
            ((AsyLoader) it.next()).ˎ(true);
        }
        linkedList.clear();
    }

    public int ॱ() {
        return this.ˎ.size();
    }

    protected void ˋ(Executor executor, AsyLoader o_cwa) {
        LinkedList linkedList = (LinkedList) this.ˎ.get(o_cwa.ॱˊ());
        if (linkedList != null && linkedList.size() > 0) {
            AsyLoader o_cwa2 = (AsyLoader) linkedList.getFirst();
            if (o_cwa2 != null && o_cwa2.ˋॱ()) {
                o_cwa2 = o_cwa2.ʻ();
                linkedList.removeFirst();
                linkedList.addFirst(o_cwa2);
                o_cwa2.executeOnExecutor(executor, new BaseRequest[]{o_cwa2.ˎ()});
                cvp.ˎ.ˋ("ServerAgentImpl", "checkAndReExecute, reExecute, method:" + o_cwa.ˎ().getMethod_() + ", requestType:" + o_cwa.ˎ().getRequestType());
            }
        }
    }

    public final void ˊ(Executor executor, AsyLoader o_cwa) {
        this.ॱ = new WeakReference(o_cwa);
        if (o_cwa.ˎ().getRequestType() == o.cvu.RequestStrategy.REQUEST_CACHE) {
            ˏ(o_cwa);
        } else {
            ˋ(executor, o_cwa);
        }
        o_cwa.ˊ((e) this);
        o_cwa.ˏ(executor);
    }

    public void ˎ(AsyLoader o_cwa) {
        try {
            ˋ(o_cwa);
        } catch (Exception e) {
            cvp.ˎ.ˊ("ServerAgentImpl", "onPostExecute processTask error");
        }
    }

    private void ˋ(AsyLoader o_cwa) {
        LinkedList linkedList = (LinkedList) this.ˎ.get(o_cwa.ॱˊ());
        if (linkedList == null || linkedList.size() <= 0) {
            cvp.ˎ.ˋ("ServerAgentImpl", "processTask, sessionCache is null, method:" + o_cwa.ˎ().getMethod_() + ", requestType:" + o_cwa.ˎ().getRequestType() + ", responseType:" + o_cwa.ॱॱ().getResponseType());
            o_cwa.ᐝ();
        } else if (o_cwa.ˎ().getRequestType() == o.cvu.RequestStrategy.REQUEST_CACHE) {
            ˎ(o_cwa, o_cwa.ॱˊ(), linkedList);
        } else {
            ॱ(o_cwa, o_cwa.ॱˊ(), linkedList);
        }
    }

    private void ˎ(AsyLoader o_cwa, String str, LinkedList<AsyLoader> linkedList) {
        Iterator it;
        if (o_cwa.ॱॱ().getResponseCode() == 0 && o_cwa.ॱॱ().getRtnCode_() == 0) {
            cvp.ˎ.ˋ("ServerAgentImpl", "processTask, RequestCacheTask responseCode is ok, notifyAll, method:" + o_cwa.ˎ().getMethod_());
            this.ˎ.remove(str);
            if (o_cwa.ˊॱ()) {
                o_cwa.ॱॱ().setResponseType(a.UPDATE_CACHE);
            }
            it = linkedList.iterator();
            while (it.hasNext()) {
                ((AsyLoader) it.next()).ᐝ();
            }
            linkedList.clear();
        } else if (o_cwa.ˊॱ()) {
            cvp.ˎ.ˏ("ServerAgentImpl", "processTask, RequestCacheTask responseCode is not ok, read cache succ, method:" + o_cwa.ˎ().getMethod_());
            r0 = (AsyLoader) linkedList.removeFirst();
            if (r0 != null) {
                r0.ᐝ();
                cvp.ˎ.ˋ("ServerAgentImpl", "processTask, firstTask.notifyResult()");
            }
            it = linkedList.iterator();
            while (it.hasNext()) {
                r0 = (AsyLoader) it.next();
                r0.ॱॱ().setResponseCode(1);
                r0.ᐝ();
            }
            o_cwa.ˋ(true);
            linkedList.clear();
            linkedList.addFirst(o_cwa);
        } else {
            this.ˎ.remove(str);
            cvp.ˎ.ˊ("ServerAgentImpl", "processTask, RequestCacheTask responseCode is not ok, read cache failed, method:" + o_cwa.ˎ().getMethod_() + ", cacheID:" + str);
            it = linkedList.iterator();
            while (it.hasNext()) {
                r0 = (AsyLoader) it.next();
                if (r0 != o_cwa) {
                    r0.ॱॱ().setResponseCode(1);
                }
                r0.ᐝ();
            }
            linkedList.clear();
        }
    }

    private void ॱ(AsyLoader o_cwa, String str, LinkedList<AsyLoader> linkedList) {
        AsyLoader o_cwa2 = (AsyLoader) linkedList.getFirst();
        if (o_cwa2 == null) {
            o_cwa.ᐝ();
        } else if (o_cwa2.ˋॱ()) {
            linkedList.remove(o_cwa);
            cvp.ˎ.ˊ("ServerAgentImpl", "processTask, RequestNetworkTask, cacheTask need retry, set curTask error, task num:" + linkedList.size() + ", method:" + o_cwa.ˎ().getMethod_());
            o_cwa.ॱॱ().setResponseCode(1);
            o_cwa.ᐝ();
        } else {
            ˏ(o_cwa);
            cvp.ˎ.ˋ("ServerAgentImpl", "processTask, RequestNetworkTask, cache task, process task num:" + linkedList.size() + ", method:" + o_cwa.ˎ().getMethod_() + ", cacheID:" + str);
        }
    }

    public void ॱ(AsyLoader o_cwa) {
        try {
            if (o_cwa.ॱˊ() != null) {
                cvp.ˎ.ˋ("ServerAgentImpl", "onCancelled, remove task");
                ॱ(o_cwa.ॱˊ());
            }
        } catch (Throwable e) {
            cvp.ˎ.ˊ("ServerAgentImpl", "onCancelled error, method:" + o_cwa.ˎ().getMethod_(), e);
        }
    }

    private void ॱ(String str) {
        LinkedList linkedList = (LinkedList) this.ˎ.remove(str);
        if (linkedList != null && linkedList.size() > 0) {
            Iterator it = linkedList.iterator();
            while (it.hasNext()) {
                AsyLoader o_cwa = (AsyLoader) it.next();
                if (this.ॱ == null || this.ॱ.get() != o_cwa) {
                    cvp.ˎ.ˋ("ServerAgentImpl", "removeSession, cacheID:" + o_cwa.ॱˊ());
                    o_cwa.ˎ(true);
                } else {
                    cvp.ˎ.ˋ("ServerAgentImpl", "currentTask == serverTask , not cancel");
                }
            }
        }
    }
}
